#ifndef __zvty_h__
#define __zvty_h__

#define VTY_BUFSIZ 512
#define VTY_MAXHIST 20

class ZVTYStubI
{
public:
	virtual int vty_auth(const char* name,const char* pwd, char** err)=0;
	virtual int str_free(char* errstr)=0;
	virtual int vty_outbuf_reset()=0;
	virtual int vty_write (const char *buf, size_t nbytes)=0;
	virtual int vty_prompt()=0;
	virtual int vty_execute()=0;
	virtual int vty_complete_command()=0;
	virtual int vty_hello()=0;
};
struct ZVTY
{
	char peeraddr[512];

	char* name;

	/* Is this vty connect to file or not */
	enum {VTY_TERM, VTY_FILE, VTY_SHELL, VTY_SHELL_SERV} type;

	enum {NAME_NODE,AUTH_NODE,XXX_NODE};
	int node;

	int beginsec;
	int loginsec;

	int idle;

	/* Failure count */
	int fail;

	/* Command input buffer */
	char *buf;

	/* Command cursor point */
	int cp;

	/* Command length */
	int length;

	/* Command max length. */
	int max;

	/* Histry of command */
	char *hist[VTY_MAXHIST];

	/* History lookup current point */
	int hp;

	/* History insert end point */
	int hindex;

	/* For escape character. */
	unsigned char escape;

	/* Current vty status. */
	enum {VTY_NORMAL, VTY_CLOSE, VTY_MORE, VTY_MORELINE} status;

	/* IAC handling: was the last character received the
	IAC (interpret-as-command) escape character (and therefore the next
	character will be the command code)?  Refer to Telnet RFC 854. */
	unsigned char iac;

	/* IAC SB (option subnegotiation) handling */
	unsigned char iac_sb_in_progress;
	/* At the moment, we care only about the NAWS (window size) negotiation,
	and that requires just a 5-character buffer (RFC 1073):
	<NAWS char> <16-bit width> <16-bit height> */
#define TELNET_NAWS_SB_LEN 5
	unsigned char sb_buf[TELNET_NAWS_SB_LEN];
	/* How many subnegotiation characters have we received?  We just drop
	those that do not fit in the buffer. */
	size_t sb_len;

	/* Window width/height. */
	int width;
	int height;
public:
	ZVTYStubI* m_stub;
public:
	void init(ZVTYStubI* stub);
	void close();
	int read(unsigned char* buf,int nbytes);
	void insert_word_overwrite(int bac,const char* str);
	void redraw_line();
};
#endif
